package com.zhou.chat;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.*;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.zhou.push.ICallBack;
import com.zhou.push.Push;
import com.zhou.push.PushService;

public class MainActivity extends Activity implements View.OnClickListener{
    private Push push;
    private EditText sendText;
    private Button sendBtn;
    private GetMsgHandler mHandler;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startService();
        sendText = (EditText) findViewById(R.id.sendText);
        sendBtn = (Button)findViewById(R.id.sendBtn);
        mHandler = new GetMsgHandler();
        sendBtn.setOnClickListener(this);
    }

    public void startService(){
        Intent pushIntent = new Intent(getApplicationContext(),PushService.class);
        bindService(pushIntent, connection, BIND_AUTO_CREATE);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            push=Push.Stub.asInterface(service);
            try {
                push.connect(new ICallBack.Stub() {
                    @Override
                    public void read(String msg) throws RemoteException {
                        Message message = mHandler.obtainMessage();
                        message.what = 1;
                        message.obj = msg;
                        mHandler.sendMessage(message);
                    }
                });
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            push = null;
        }
    };


    private class GetMsgHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Toast.makeText(MainActivity.this,"--->"+(String)msg.obj,Toast.LENGTH_LONG).show();
                    ;break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sendBtn:
                if(push!=null){
                    try {
                        if(push.sendMsg(sendText.getText().toString())){
                            Toast.makeText(this,"发送成功",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(this,"发送失败",Toast.LENGTH_SHORT).show();
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                        System.out.println("远程问题");
                    }
                }else{
                    Toast.makeText(this,"push=null 正在尝试重新启动service ，请稍后重试！",Toast.LENGTH_SHORT).show();
                    startService();
                }
                break;
        }
    }
}
